package com.ashin.leetcode;

import org.junit.Test;

/**
 * 最长回文字串
 *
 * @author Ashin
 */
public class L005LongestPalindrome {


    @Test
    public void test() {
        String s = "";
        System.out.println(longestPalindrome(s));
    }


    public String longestPalindrome(String s) {

        int max = 0;
        String ans = "";
        for (int i = 0; i < s.length(); i++) {
            for (int j = i + 1; j <= s.length(); j++) {
                String test = s.substring(i, j);
                if (isPalindrome(test) && test.length() > max) {
                    ans = test;
                    max = test.length();
                }
            }
        }

        return ans;
    }

    public boolean isPalindrome(String s) {
        for (int i = 0; i < s.length() / 2; i++) {
            if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
                return false;
            }
        }
        return true;
    }


}
